package com.lb.videoplay.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lb.videoplay.R;
import com.lb.videoplay.activity.FeedbackActivity;
import com.lb.videoplay.activity.IntroductionActivity;
import com.lb.videoplay.activity.LoginActivity;
import com.lb.videoplay.activity.MainActivity;
import com.lb.videoplay.model.UserInfoModel;
import com.lb.videoplay.utils.CommonUtils;
import com.lb.videoplay.utils.DataCleanManager;
import com.lb.videoplay.utils.SharedPref;
import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
import com.othershe.nicedialog.ViewConvertListener;
import com.othershe.nicedialog.ViewHolder;

/**
 * Created by Mr.lb
 * 时间： 2018/11/15
 * 包名： com.lb.videoplay.activity
 * 描述： Me Fragment
 */
public class MeFragment extends Fragment implements View.OnClickListener {

    private TextView mCentent;
    private TextView mRightIcon;
    private ImageView mIvHead;
    private TextView mTvName;
    private LinearLayout mLayoutClear;
    private LinearLayout mLayoutCommon;
    private LinearLayout mLayoutVersion;
    private LinearLayout mLayoutBrand;
    private CommonUtils commonUtils;
    private TextView mTvClear;
    private TextView mTvVersion;

    private boolean mLogin = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //通过参数中的布局填充获取对应布局
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        initWidget(view);
        initEvent();
        initData();
        mTvVersion.setText("v" + AppUtils.getAppVersionName());
        return view;
    }

    //初始化控件
    private void initWidget(View view) {
        commonUtils = new CommonUtils(getActivity());
        mCentent = view.findViewById(R.id.center_title);
        mIvHead = view.findViewById(R.id.iv_user_head);
        mTvName = view.findViewById(R.id.tv_name);
        mLayoutClear = view.findViewById(R.id.ll_clear_cache);
        mLayoutBrand = view.findViewById(R.id.ll_brand);
        mLayoutCommon = view.findViewById(R.id.ll_common_problem);
        mLayoutVersion = view.findViewById(R.id.ll_version);
        mTvClear = view.findViewById(R.id.tv_clear);
        mRightIcon = view.findViewById(R.id.right_title_icon);
        mTvVersion = view.findViewById(R.id.tv_version);
    }

    //点击事件
    private void initEvent() {
        mLayoutClear.setOnClickListener(this);
        mLayoutBrand.setOnClickListener(this);
        mLayoutCommon.setOnClickListener(this);
        mLayoutVersion.setOnClickListener(this);
        mRightIcon.setOnClickListener(this);
        mRightIcon.setTextColor(getResources().getColor(R.color.baseres_bt_color));
    }

    //初始化
    public void initData() {
        mCentent.setText(getString(R.string.tab_me));
        if (ObjectUtils.isEmpty(SPUtils.getInstance().getString("phone"))) {
            mIvHead.setImageResource(R.drawable.ic_avatar_default);
            mTvName.setText("未登录");
            mRightIcon.setText("登录");
            mLogin = false;
        } else {
            CommonUtils commonUtils = new CommonUtils(getActivity());
            if (SPUtils.getInstance().getBoolean("isLogin")) {
                mIvHead.setImageBitmap(commonUtils.base64ToBitmap(SPUtils.getInstance().getString("userhead")));
                mTvName.setText(SPUtils.getInstance().getString("userName"));
                mRightIcon.setText("退出登录");
                mLogin = true;
            } else {
                mIvHead.setImageResource(R.drawable.ic_avatar_default);
                mTvName.setText("未登录");
                mRightIcon.setText("登录");
                mLogin = false;
            }
        }
        try {
            mTvClear.setText(String.format("%s", DataCleanManager.getTotalCacheSize(getActivity())));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void clearCache() {
        NiceDialog.init()
                .setLayoutId(R.layout.item_dialog)     //设置dialog布局文件
                .setConvertListener(new ViewConvertListener() {     //进行相关View操作的回调
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        holder.setText(R.id.tv_content, getString(R.string.app_me_clear));
                        holder.setText(R.id.tv_submit, getString(R.string.app_confirm));
                        holder.setTextColor(R.id.tv_cancel, getResources().getColor(R.color.baseres_size_color_three_title));
                        holder.setOnClickListener(R.id.tv_submit, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                cacheSize(mTvClear);
                            }
                        });
                        holder.setOnClickListener(R.id.tv_cancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                    }
                })
                .setDimAmount(0.3f)     //调节灰色背景透明度[0-1]，默认0.5f
                .setShowBottom(false)     //是否在底部显示dialog，默认flase
                .setMargin(50)     //dialog左右两边到屏幕边缘的距离（单位：dp），默认0dp
                .setOutCancel(true)     //点击dialog外是否可取消，默认true
                .show(getActivity().getSupportFragmentManager());//显示dialog
    }

    //获取缓存大小
    public void cacheSize(TextView tv_clear) {
        DataCleanManager.clearAllCache(getActivity());

        commonUtils.showLoadDialog("正在清除");
        tv_clear.postDelayed(() -> {
            commonUtils.dissDialog();
            ToastUtils.showLong(getString(R.string.app_me_clear_cache));
            try {
                tv_clear.setText(String.format("%s", DataCleanManager.getTotalCacheSize(getActivity())));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 1000);

    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_brand: //品牌介绍
                ActivityUtils.startActivity(IntroductionActivity.class);
                break;
            case R.id.ll_common_problem: //意见反馈
                ActivityUtils.startActivity(FeedbackActivity.class);
                break;
            case R.id.ll_clear_cache: //清除缓存
                clearCache();
                break;
            case R.id.ll_version: //版本更新
                commonUtils.showToastCenter(getString(R.string.app_me_new_version));
                break;
            case R.id.right_title_icon:  //登录
                if (!mLogin) {
                    ActivityUtils.startActivity(LoginActivity.class);
                } else { //退出登录 清空登录信息
                    mIvHead.setImageResource(R.drawable.ic_avatar_default);
                    SPUtils.getInstance().put("isLogin", false);
                    mTvName.setText("未登录");
                    mRightIcon.setText("登录");
                    mLogin = false;
                }
                break;

        }
    }
}
